package com.luo.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.luo.shop.dao.*;
import com.luo.shop.pojo.*;
import com.luo.shop.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.swing.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private CartDao cartDao;
    @Autowired
    private GoodsBoDao goodsBoDao;

    public List<Comment> queryByGid(Long gid) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("createtime desc");
        criteria.andEqualTo("gid",gid);
        example.and(criteria);
        List<Comment> commentList = commentDao.selectByExample(example);
        for (Comment comm : commentList) {
            if (comm.getPid() == 0) {
                comm.setUser(userDao.selectByPrimaryKey(comm.getUid()));
                comm.setSomComment(queryByPid(comm.getId(), gid));
                comm.setGoods(goodsDao.selectByPrimaryKey(comm.getGid()));
                if (comm.getImages() != null) {
                    comm.setImage(Arrays.asList(comm.getImages().split("~")));
                }
                Cart cart = new Cart();
                cart.setGid(gid);
                cart.setUid(comm.getUid());
                if (cartDao.select(cart).size()>0) {
                    comm.setGoodsBoType(goodsBoDao.selectByPrimaryKey(cartDao.select(cart).get(0).getGoodsBoId()).getType());
                }
            }

        }
        return commentList;
    }

    public List<Comment> queryByPid(Long pid, Long gid) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("createtime desc");
        criteria.andEqualTo(pid);
        List<Comment> select = commentDao.selectByExample(example);
        for (Comment comm : select) {
            comm.setUser(userDao.selectByPrimaryKey(comm.getUid()));
            comm.setGoods(goodsDao.selectByPrimaryKey(comm.getGid()));
            if (comm.getImages() != null) {
                comm.setImage(Arrays.asList(comm.getImages().split("~")));
            }
            Cart cart = new Cart();
            cart.setGid(gid);
            cart.setUid(comm.getUid());
            if (cartDao.select(cart).size()>0){
                comm.setGoodsBoType(goodsBoDao.selectByPrimaryKey(cartDao.select(cart).get(0).getGoodsBoId()).getType());
            }
        }

        return select;
    }

    public void save(Comment comment) {
        if (comment.getPid() == null) {
            comment.setPid((long) 0);
        }
        comment.setCreatetime(new Date());
        commentDao.insertSelective(comment);

    }

    @Transactional
    public Integer del(Long id) {
        Comment comment = commentDao.selectByPrimaryKey(id);
        Comment comm = new Comment();
        comm.setPid(id);
        commentDao.delete(comm);
        return commentDao.deleteByPrimaryKey(id);
    }
}
